<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>火车车次信息管理系统</title>

    <style>
        /*设置奇数行颜色*/
        /*table tr:nth-child(odd)*/
        /*{*/
        /*    background: #ccc;*/
        /*}*/

        /*设置偶数行颜色*/
        table tr:nth-child(even) {
            background: #c1c1c1;
        }
    </style>
</head>
<body>
<div style="text-align: center;">
    <table width="80%" border="1" cellspacing="0" cellpadding="0" style="margin: auto" id="dataTable">
        <tr>
            <th><a href="#" onclick="deleteChecked()">删除</a></th>
            <th>车次</th>
            <th>发车--到达</th>
            <th>发时--到时</th>
            <th>车型</th>
            <th>运行时间(小时)</th>
        </tr>
    </table>
    <input type="checkbox"  name="selectAll" id="selectAll" onclick="selectAll(this);"> 全选
</div>
</body>
<script type="text/javascript" src="jquery-2.2.3.min.js"></script>
<script>
    $(function () {
        function onload() {
            let table = $("#dataTable");
            $.get("train/getAllTrain", {

            }, function (rtn) {
                if (rtn.code != 0) {
                    alert(rtn.msg);
                    return;
                }
                $.each(rtn.data, function (index, d) {
                    table.append(
                        "<tr>" +
                        "<td><input type='checkbox' name='checkId' id='checkId' value='"+d.train_no+"'></td>" +
                        "<td><a href='train/findTrain/"+d.train_no+"'>" + d.train_no + "</a></td>" +
                        "<td>" + d.start_station +"--"+d.arrival_station+"</td>" +
                        "<td>" + d.start_time +"--"+d.arrival_time+"</td>" +
                        "<td>" + d.type + "</td>" +
                        "<td>" + d.runtime + "</td>" +
                        "</tr>")
                });
            });

        };
        onload();
    });
    /**
     * checkbox全选/全不全
     */
    function selectAll(checkbox) {
        $('input[name="checkId"]').prop('checked', $(checkbox).prop('checked'));
    }
    /**
     * 批量删除
     */
    function deleteChecked() {
        let checkNum = $("input[name='checkId']:checked").length;
        if(checkNum==0){
            alert("请至少选择一项车次！");
            return;
        }

        if(confirm("确定要删除吗？")){
            var checkList = new Array();
            $("input[name='checkId']:checked").each(function () {
                checkList.push($(this).val());
            });
            // checkList = checkList.slice(0,checkList.length-1);
            // alert("checkList:"+checkList)
        }

        $.ajax({
            url:"train/delTrain",
            type:"post",
            data:{
                trainId:checkList.toString()
            },
            datatype:"json",
            success:function (data) {
                location.reload();
                alert(data.msg)
            },
            error:function (data) {
                alert(data.msg+"删除失败！")
            }
        })
    }

</script>
</html>